home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12217 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  75 lines

  1. Path: web.cae.ca!usenet
  2. From: fraserh@cae.ca (Fraser Hutchinson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q]Dereference within class - Possible?
  5. Date: 18 Mar 1996 19:38:15 GMT
  6. Organization: CAE Electronics Ltd.
  7. Message-ID: <4ike37$rck@web.cae.ca>
  8. References: <Do566t.F1H.0.queen@torfree.net>
  9. NNTP-Posting-Host: pch63.cae.ca
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In article <Do566t.F1H.0.queen@torfree.net>, bh332@freenet.toronto.on.ca 
  15. says...
  16. >
  17. >
  18. >
  19. >Greetings,
  20. >
  21. >In normal C style, one can code the following example(C++ compiler);
  22. >
  23. >int main() {
  24. >  int x   = 1;
  25. >  int &x1 = x;
  26. >
  27. >  ...
  28. >  ...
  29. >}
  30. >
  31. >Is it also possible to use the same syntax within a class without intializing
  32. >the referencer? Any information will be appreciated...
  33.  
  34. No, it is not.  Classes are similar to struct definitions; they must be 
  35. instantiated before they can be initialized.  This is what constructors do. 
  36.  
  37. For example, in C I doubt you would consider this:
  38.  
  39. struct foo {
  40.   int x = 100;
  41.   char text[] = "my text string";
  42. };
  43.  
  44. What you are doing is somewhat analogous to
  45.  
  46. class Int {
  47.   Int(Int &x)        { val = x.GetVal(); }
  48.   Int(int)
  49.  
  50.   int  GetVal()        { return x; }
  51.   void SetVal(int x)    { val = x;  }
  52.  
  53. protected:
  54.   int val;
  55. }
  56.  
  57. Int::Int(int x) : val(x) {}
  58.  
  59.  
  60. void main() {
  61.  
  62.    Int x(4)
  63.    Int *y(x);
  64.  
  65.  
  66. }
  67.  
  68. Hope I read this one right,
  69.  
  70. regards,
  71.  
  72. Fraser
  73.  
  74.  
  75.